<?php

namespace App\Http\Controllers\School;

use App\Http\Requests\School\EditBookRule;
use App\Http\Requests\School\SettleBookRule;
use App\Http\Requests\School\StoreBookRule;
use App\Http\Resources\School\BookRuleResource;
use App\Jobs\AutomaticOrdering;
use App\Jobs\BookRuleSettle;
use App\Models\BookRule;
use App\Models\SchoolAdminLog;
use App\Services\BookRuleService;
use Illuminate\Http\Request;

/**
 *
 * 订餐规则管理
 *
 * Class BookRuleController
 * @package App\Http\Controllers\School
 */
class BookRuleController extends BaseController
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     * 订餐规则列表
     */
    public function index(Request $request)
    {
        $bookRules = BookRule::orderBy('created_at', 'desc')
            ->with('foods')->with('prices.category')
            ->where('school_id', \Auth::user()->school_id)
            ->eatTypeIs($request->eat_type)
            ->eatDateIs($request->eat_date)
            ->isCancel($request->is_cancel)
            ->isSettled($request->is_settled)
            ->paginate($request->size);
        return BookRuleResource::collection($bookRules);
    }

    /**
     * @param StoreBookRule $request
     * @return BookRuleResource|array
     * @throws \Exception
     * 新增订餐规则
     */
    public function store(StoreBookRule $request)
    {
        $bookRule = BookRuleService::add($request);
        // 进入队列 - 给符合条件的用户自动加单
        AutomaticOrdering::dispatch($bookRule);
        $eatType = $bookRule->getTypeLabelAttribute();
        SchoolAdminLog::Log('添加订餐规则: ' . $request->eat_date . ' - ' . $eatType);
        return new BookRuleResource($bookRule);
    }

    /**
     * @param EditBookRule $request
     * @param $id
     * @return BookRuleResource|array
     * @throws \Exception
     * 编辑订餐规则
     */
    public function update(EditBookRule $request, $id)
    {
        $bookRule = BookRule::where('school_id', \Auth::user()->school_id)->findOrFail($id);
        if ($bookRule->is_cancel === BookRule::CANCELED) {
            return $this->fail('当前订餐规则已被取消，操作失败!');
        }
        $bookRule = BookRuleService::edit($bookRule);
        $eatType = $bookRule->getTypeLabelAttribute();
        SchoolAdminLog::Log('修改订餐规则: ' . $bookRule->eat_date . ' - ' . $eatType);
        return new BookRuleResource($bookRule);
    }

    /**
     * @param SettleBookRule $request
     * @param $id
     * @return BookRuleResource|array
     * * 结算此订餐规则
     * 1.标记此订餐规则下的所有订单为已计算餐费
     * 2.填充每笔订单的原价为 条目总价 / 订单数
     * 3.填充每笔订单的计费规则为 对应的订单规则中的订餐类型去查找用户角色表中的计费规则
     * 4.订单实际支付价格为
     * 4.1若为固定餐标，则实际支付 = user_role表中的固定餐费
     * 4.2若为计算餐标，则实际支付 = 原价 * 付款比例（早、中、晚）
     * 5.订单状态为normal，正常结算
     * 5.1订单状态为addition时，按订单原价结算
     * 5.2订单状态为official时，不参与结算
     * 5.3订单状态为children时，按订单原价结算
     */
    public function settle(SettleBookRule $request, $id)
    {
        $bookRule = BookRule::where('school_id', \Auth::user()->school_id)->findOrFail($id);
        if ($bookRule->is_cancel === BookRule::CANCELED) {
            return $this->fail('当前订餐规则已被取消，操作失败!');
        }
        if ($bookRule->is_settled === BookRule::SETTLED) {
            return $this->fail('当前订餐规则已结算，操作失败!');
        }
        // 进入队列 - 结算当前订餐规则 和 该订餐规则下的所有订单
        BookRuleSettle::dispatch($bookRule);
        $eatType = $bookRule->getTypeLabelAttribute();
        SchoolAdminLog::Log('结算订餐规则: ' . $bookRule->eat_date . ' - ' . $eatType);
        return new BookRuleResource($bookRule);
    }

    /**
     * @param BookRule $bookRule
     * @return BookRuleResource|array
     * @throws \Exception
     * 取消订餐规则
     */
    public function destroy(BookRule $bookRule)
    {
        $eatType = $bookRule->getTypeLabelAttribute();
        // 判断此订餐规则状态
        if ($bookRule->is_cancel === BookRule::CANCELED) {
            return $this->fail('当前订餐规则已被取消, 操作失败!');
        }
        // 订餐规则已结算不能取消
        if ($bookRule->is_settled === BookRule::SETTLED) {
            return $this->fail('已结算的订餐规则不能取消, 操作失败!');
        }
        $bookRule = BookRuleService::cancel($bookRule);
        SchoolAdminLog::Log('取消订餐规则: ' . $bookRule->eat_date . ' - ' . $eatType);
        return new BookRuleResource($bookRule);
    }
}
